/**
 * Copyright (c) 2013-5-9 下午5:11:08 WKM and/or its affiliates. 
 * Use is subject to license terms.
 * All rights reserved.
 */
package org.extblog.wkm.web.action.article;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.extblog.wkm.web.action.ActionController;
import org.extblog.wkm.web.service.article.ArticleService;
import org.extblog.wkm.web.vo.ArticleView;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author WKM
 * Service.java history
 * 1.WKM - 2013-5-9 - Create File
 */
@Controller
@RequestMapping("/article")
public class ArticleController extends ActionController {

	private ArticleService articleService = null;
	
	public ArticleService getArticleService() {
		return articleService;
	}

	@Resource
	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}

	@RequestMapping("update.wkm")
	public ModelAndView addArticle(@ModelAttribute("view") ArticleView view){
		ModelAndView mav = new ModelAndView();
		if(StringUtils.isBlank(view.getArticleId())){
			if(articleService.addArticle(view)){
				mav.addObject("messageForm0", "添加文章成功！");
			} else {
				mav.addObject("messageForm0", "添加文章失败！");
			}
		} else {
			if(articleService.updateArticle(view)){
				mav.addObject("messageForm0", "更新文章成功！");
			} else {
				mav.addObject("messageForm0", "更新文章失败！");
			}
		}
		mav.setViewName("article/add");
		return mav;
	}
	
	@RequestMapping("articlePage.wkm")
	public ModelAndView articlePage(@ModelAttribute("view") ArticleView view){
		ModelAndView mav = new ModelAndView();
		mav.setViewName("article/add");
		if(StringUtils.isBlank(view.getArticleId())){
			log.info("跳转到页面添加页面...");
			mav.addObject("messageForm0", "添加文章！");
		} else {
			log.info("加载文章:" + view.getArticleId());
			List<ArticleView> list = articleService.loadArticle(view);
			if(list == null){
				log.info("加载文章失败！");
				mav.addObject("messageForm0", "加载文章失败！");
				return mav;
			}
			if(list.isEmpty()){
				log.info("没有找到所指定的文章！");
				mav.addObject("messageForm0", "没有找到所指定的文章！");
				return mav;
			}
			log.info("加载文章个数:" + list.size());
			mav.addObject("articleId", list.get(0).getArticleId());
			mav.addObject("articleName", list.get(0).getArticleName());
			mav.addObject("articleContent", list.get(0).getArticleContent());
			mav.addObject("messageForm0", "加载文章成功！");
		}
		return mav;
	}
	
	@RequestMapping("list.wkm")
	public ModelAndView articleList(@ModelAttribute("view") ArticleView view){
		ModelAndView mav = new ModelAndView();
		log.info("加载文章...");
		List<ArticleView> list = articleService.loadArticle(view);
		mav.addObject("list", list);
		if(list != null && list.size() > 0) {
			mav.addObject("articleId",list.get(0).getArticleId());
		}
		if(!StringUtils.isBlank(view.getArticleId())){
			mav.addObject("articleId",view.getArticleId());
		}
		mav.setViewName("article/list");
		return mav;
	}
	
	@RequestMapping("edit.wkm")
	public ModelAndView articleEditList(@ModelAttribute("view") ArticleView view){
		ModelAndView mav = new ModelAndView();
		log.info("加载文章...");
		List<ArticleView> list = articleService.loadArticle(view);
		mav.addObject("list", list);
		mav.setViewName("article/edit");
		return mav;
	}
	
	@RequestMapping("show.wkm")
	public ModelAndView articleShow(@ModelAttribute("view") ArticleView view){
		ModelAndView mav = new ModelAndView();
		log.info("加载文章:" + view.getArticleId());
		List<ArticleView> list = articleService.loadArticle(view);
		if(list == null){
			mav.setViewName("article/show");
			mav.addObject("message", "文章不存在或已被删除！");
			return mav;
		}
		if(list.size() == 0){
			mav.setViewName("article/show");
			mav.addObject("message", "文章不存在或已被删除！");
			return mav;
		}
		mav.addObject("articleId", list.get(0).getArticleId());
		mav.addObject("articleName", list.get(0).getArticleName());
		mav.addObject("articleContent", list.get(0).getArticleContent());
		mav.setViewName("article/show");
		return mav;
	}
	
}
